#include"ChatClient.h"

int getpost(int sockfd)
{
int i=1;
string msg,sbuff;
while(1)
{
sbuff="ok";
if(datadeal(sockfd,sbuff)=="end")
break;
cout<<i++<<" ";
sbuff="first";
msg=datadeal(sockfd,sbuff);
cout<<msg<<"   ";
sbuff="second";
msg=datadeal(sockfd,sbuff);
cout<<msg<<"  ";
sbuff="third";
msg=datadeal(sockfd,sbuff);
cout<<msg<<endl;
}
}
void detail(int sockfd)
{
string msg,sbuff;
sbuff="ok";
for(int i=0;i<4;i++)
{
msg=datadeal(sockfd,sbuff);
cout<<msg<<endl;
}
}

void post(int sockfd)
{
string i,num;
string input,title,body,sbuff;
//输出所有序号，用户名，主题
while(1)
{
num=getpost(sockfd);
cout<<"add post or seemore?or back"<<endl;
cin>>input;
if(input=="seemore")
{
if(datadeal(sockfd,input)!="seemore") 
{cout<<"post error"<<endl;
break;}
cout<<"which?"<<endl;
cin>>i;
if(i<=num&&i>"0")
{if(datadeal(sockfd,i)=="ok")
cout<<"into deatal..."<<endl;
detail(sockfd);}
else {
cout<<"did`t exist"<<endl;
sbuff="error";
if(datadeal(sockfd,sbuff)!="error")
break;
}}
else if(input=="add")
{
if(datadeal(sockfd,input)!="add") 
{cout<<"post error"<<endl;
break;
}
cout<<"please input a title ："<<endl;
//cin.ignore();
cin>>title;
if(datadeal(sockfd,title)!="title")
{
cout<<"error in post"<<endl;
break;
}
cout<<"please input body : or\"end\""<<endl;
input="";
while(input!="end")
{
getline(cin,input);
body+=input+"\n";}
if(datadeal(sockfd,body)!="body")
{
cout<<"error in post"<<endl;
break;
}
//sent time 
if(datadeal(sockfd,gettime())!="time")
{
cout<<"error in post"<<endl;
break;
}
cout<<"add post successfully"<<endl;
}
else if(input=="back")
{
if(datadeal(sockfd,input)=="back")
break;}
else cout<<"command not found"<<endl;
}
}
